package com.hbnu.zhanglian.client.ui;

import com.hbnu.zhanglian.client.network.GameConnection;
import com.hbnu.zhanglian.service.UserService;
import com.hbnu.zhanglian.entity.User;
import java.util.Scanner;

public class ConsoleUI {
    private Scanner scanner = new Scanner(System.in);
    private UserService userService = new UserService();
    private User currentUser;

    public void showMainMenu() {
        while (true) {
            System.out.println("\n=== 你画我猜游戏 ===");
            System.out.println("1. 注册");
            System.out.println("2. 登录");
            System.out.println("3. 退出");
            System.out.print("请选择: ");

            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    register();
                    break;
                case "2":
                    login();
                    break;
                case "3":
                    System.out.println("再见！");
                    return;
                default:
                    System.out.println("无效选择");
            }
        }
    }

    private void register() {
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        if (username.isEmpty() || password.isEmpty()) {
            System.out.println("用户名和密码不能为空");
            return;
        }

        boolean success = userService.register(username, password);
        if (success) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败，用户名可能已存在");
        }
    }

    private void login() {
        System.out.print("用户名: ");
        String username = scanner.nextLine();
        System.out.print("密码: ");
        String password = scanner.nextLine();

        User user = userService.login(username, password);
        if (user != null) {
            currentUser = user;
            System.out.println("欢迎 " + user.getUsername());
            startGame();
        } else {
            System.out.println("登录失败");
        }
    }

    private void startGame() {
        System.out.println("\n连接游戏服务器...");
        GameConnection connection = new GameConnection();

        if (connection.connect("localhost", 8888)) {
            System.out.println("连接成功！");

            // 发送用户名
            connection.sendMessage(currentUser.getUsername());

            // 启动消息监听
            connection.startMessageListening(message -> {
                System.out.println(message);
            });

            // 简单的游戏循环
            System.out.println("等待游戏开始...");
            while (true) {
                String input = scanner.nextLine();

                if (input.equals("quit")) {
                    break;
                }

                connection.sendMessage(input);
            }

            connection.disconnect();
        } else {
            System.out.println("连接失败");
        }

        currentUser = null;
    }
}